<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {

    protected function config($key)
    {
        return config('admin-api.database.' . $key);
    }

    /**
     * Run the migrations.
     */
    public function up(): void
    {
        foreach ($this->getSqlArr() as $table => $sql) {
            dump($sql);
            DB::statement($sql);
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        foreach ($this->getSqlArr() as $table => $sql) {
            Schema::dropIfExists($table);
        }
    }

    protected function getSqlArr(): array
    {
        return [
            $this->config('user_table') => <<<sql
CREATE TABLE `{$this->config('user_table')}` (
   `id` int unsigned NOT NULL AUTO_INCREMENT,
   `nickname` varchar(32) NOT NULL DEFAULT '' COMMENT '昵称',
   `account` varchar(32) NOT NULL COMMENT '账号',
   `password` varchar(64) NOT NULL COMMENT '密码',
   `avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '头像',
   `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`),
   UNIQUE KEY `{$this->config('user_table')}_account_unique` (`account`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '用户表';
sql,
            $this->config('role_table') => <<<sql
CREATE TABLE `{$this->config('role_table')}` (
   `id` int unsigned NOT NULL AUTO_INCREMENT,
   `name` varchar(32) NOT NULL COMMENT '',
   `slug` varchar(32) NOT NULL COMMENT '',
   `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`),
   UNIQUE KEY `{$this->config('role_table')}_slug_unique` (`slug`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '角色表';
sql,
            $this->config('menu_table') => <<<sql
CREATE TABLE `{$this->config('menu_table')}` (
   `id` int unsigned NOT NULL AUTO_INCREMENT,
   `pid` int unsigned NOT NULL DEFAULT '0',
   `title` varchar(32) NOT NULL,
   `code` varchar(32) not null default '',
   `component` varchar(64) NOT NULL default '',
   `type` tinyint NOT NULL DEFAULT '1' COMMENT '1:目录 2:菜单 3:按钮',
   `icon` varchar(32) DEFAULT NULL DEFAULT '',
   `path` varchar(64) NOT NULL default '',
   `redirect_path` varchar(64) DEFAULT NULL,
   `order` smallint NOT NULL DEFAULT '0',
   `always_show` tinyint NOT NULL DEFAULT '0' COMMENT 'alwaysShow 0 false 1 true',
   `hidden` tinyint NOT NULL DEFAULT '0' COMMENT '隐藏 0 false 1 true',
   `disable` tinyint not null default 0 comment '状态',
   `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '菜单表';
sql,
            $this->config('permission_table') => <<<sql
CREATE TABLE `{$this->config('permission_table')}` (
   `id` int unsigned NOT NULL AUTO_INCREMENT,
    `pid` int(10) unsigned NOT NULL DEFAULT '0',
    `label` varchar(128) NOT NULL default '',
    `uri` varchar(64) NOT NULL default '',
    `methods` varchar(16) NOT NULL default '',
    `slug` varchar(32) NOT NULL comment'method+uri',
    `order` smallint(6) NOT NULL DEFAULT '0',
    `type` tinyint not null default 0,
    `remark` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL default '',
   `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   PRIMARY KEY (`id`),
   UNIQUE KEY `{$this->config('permission_table')}_slug_unique` (`slug`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '权限表';
sql,
            $this->config('user_role_table') => <<<sql
CREATE TABLE `{$this->config('user_role_table')}` (
   `user_id` int unsigned NOT NULL,
   `role_id` int unsigned NOT NULL,
   `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   UNIQUE KEY `{$this->config('user_role_table')}_user_id_role_id_unique` (`user_id`, `role_id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '';
sql,
            $this->config('role_menu_table') => <<<sql
CREATE TABLE `{$this->config('role_menu_table')}` (
   `role_id` int unsigned NOT NULL,
   `menu_id` int unsigned NOT NULL,
   `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   UNIQUE KEY `{$this->config('role_menu_table')}_role_id_menu_id_unique` (`role_id`,`menu_id`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '';
sql,
            $this->config('role_permission_table') => <<<sql
CREATE TABLE `{$this->config('role_permission_table')}` (
   `role_id` int unsigned NOT NULL,
   `permission_key` varchar(32) NOT NULL,
   `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
   `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
   UNIQUE KEY `{$this->config('role_permission_table')}_role_id_permission_key_unique` (`role_id`,`permission_key`)
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT '';
sql,
        ];
    }
};
